<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/Peppa.png?v=7.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/Peppa.jpg?v=7.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/Peppa.jpg?v=7.0.0">








<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="1. A - Multi-Subject Competition CodeForces - 1082C1.1 题目大意给定n个考生，每个考生都有唯一选择的科目$s_i$和对应的能力水平$r_i$。同时给定m个考试科目。 求选定若干个科目，并且每个科目参与的考生数目相同的情况下，可以得到的最大能力水平和。">
<meta name="keywords" content="ACM,Tutorial">
<meta property="og:type" content="article">
<meta property="og:title" content="20190206 新生培训期末总结 题解">
<meta property="og:url" content="https://linxi99.gitee.io/20190211/20190206-新生培训期末总结-题解/index.html">
<meta property="og:site_name" content="linxi&#39;s dream">
<meta property="og:description" content="1. A - Multi-Subject Competition CodeForces - 1082C1.1 题目大意给定n个考生，每个考生都有唯一选择的科目$s_i$和对应的能力水平$r_i$。同时给定m个考试科目。 求选定若干个科目，并且每个科目参与的考生数目相同的情况下，可以得到的最大能力水平和。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-12-10T01:37:10.907Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="20190206 新生培训期末总结 题解">
<meta name="twitter:description" content="1. A - Multi-Subject Competition CodeForces - 1082C1.1 题目大意给定n个考生，每个考生都有唯一选择的科目$s_i$和对应的能力水平$r_i$。同时给定m个考试科目。 求选定若干个科目，并且每个科目参与的考生数目相同的情况下，可以得到的最大能力水平和。">






  <link rel="canonical" href="https://linxi99.gitee.io/20190211/20190206-新生培训期末总结-题解/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>20190206 新生培训期末总结 题解 | linxi's dream</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b4099cd89c9f33abab06a03748dac801";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">linxi's dream</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">穷且益坚，不坠青云之志</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://linxi99.gitee.io/20190211/20190206-新生培训期末总结-题解/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="linxi">
      <meta itemprop="description" content="stay hungry, stay foolish">
      <meta itemprop="image" content="/images/star.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="linxi's dream">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">20190206 新生培训期末总结 题解

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-02-11 16:32:30" itemprop="dateCreated datePublished" datetime="2019-02-11T16:32:30+08:00">2019-02-11</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-12-10 09:37:10" itemprop="dateModified" datetime="2019-12-10T09:37:10+08:00">2019-12-10</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/Contests/" itemprop="url" rel="index"><span itemprop="name">Contests</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/20190211/20190206-新生培训期末总结-题解/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/20190211/20190206-新生培训期末总结-题解/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
            <span id="/20190211/20190206-新生培训期末总结-题解/" class="leancloud_visitors" data-flag-title="20190206 新生培训期末总结 题解">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              
                <span class="post-meta-item-text">阅读次数：</span>
              
                <span class="leancloud-visitors-count"></span>
            </span>
            <span class="post-meta-divider">|
            </span> <span title="post.wordcount">
            <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
            </span>字数： 6.2k
            </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="1-A-Multi-Subject-Competition-CodeForces-1082C"><a href="#1-A-Multi-Subject-Competition-CodeForces-1082C" class="headerlink" title="1. A - Multi-Subject Competition CodeForces - 1082C"></a>1. A - Multi-Subject Competition CodeForces - 1082C</h1><h2 id="1-1-题目大意"><a href="#1-1-题目大意" class="headerlink" title="1.1 题目大意"></a>1.1 题目大意</h2><p>给定n个考生，每个考生都有唯一选择的科目$s_i$和对应的能力水平$r_i$。同时给定m个考试科目。</p>
<p>求选定若干个科目，并且每个科目参与的考生数目相同的情况下，可以得到的最大能力水平和。</p>
<a id="more"></a>
<h2 id="1-2-解题思路"><a href="#1-2-解题思路" class="headerlink" title="1.2 解题思路"></a>1.2 解题思路</h2><p>我们首先将对应的考生预处理到对应的考试科目当中，并将其按照能力水平降序排列。</p>
<p>只要我们知道选定k个人的最大能力值和$sum[k]$是多少，我们只需枚举k从1到m，$ans = max{sum[k]}$，我们便可以得到答案。</p>
<p>下面考虑如何获得k个人的最大能力值和</p>
<p>首先对于每一门科目维护一下能力值和，我们可以得到某一门科目选1个人的最大能力值和，选2个人的最大能力值和… …一直枚举到这门科目的所有考生，或者枚举到能力值和小于零。</p>
<p>我们考虑只要第 $i$ 门科目的选取k个人的能力值和大于零，我们在选定考试人数为k人时，将这门科目加入考虑范围一定更优，于是我们可以让$sum[k] += cal(i, k)$</p>
<h2 id="1-3-代码实现"><a href="#1-3-代码实现" class="headerlink" title="1.3 代码实现"></a>1.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 22时18分43秒</span></span><br><span class="line"><span class="comment">File Name     :A.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; v[maxn];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a &gt; b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">int</span> sum[maxn];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    <span class="keyword">int</span> n, m; sdd(n, m);</span><br><span class="line">    <span class="keyword">while</span>(n--)&#123;</span><br><span class="line">        <span class="keyword">int</span> s, r; sdd(s, r);</span><br><span class="line">        v[s].pb(r);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> mx = <span class="number">0</span>;</span><br><span class="line">    rep(i, <span class="number">1</span>, m + <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">int</span> sz = (<span class="keyword">int</span>)v[i].size();</span><br><span class="line">        <span class="keyword">if</span>(sz &gt; mx) mx = sz;</span><br><span class="line">        sort(v[i].begin(), v[i].end(), cmp);</span><br><span class="line">        <span class="keyword">int</span> now = <span class="number">0</span>;</span><br><span class="line">        rep(j, <span class="number">0</span>, sz)&#123;</span><br><span class="line">            now += v[i][j];</span><br><span class="line">            <span class="keyword">if</span>(now &gt; <span class="number">0</span>) sum[j + <span class="number">1</span>] += now;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">    rep(i, <span class="number">0</span>, mx + <span class="number">1</span>) ans = max(ans, sum[i]);</span><br><span class="line">    pd(ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="2-B-Minimizing-the-String-CodeForces-1076A"><a href="#2-B-Minimizing-the-String-CodeForces-1076A" class="headerlink" title="2 B - Minimizing the String CodeForces - 1076A"></a>2 B - Minimizing the String CodeForces - 1076A</h1><h2 id="2-1-题目大意"><a href="#2-1-题目大意" class="headerlink" title="2.1 题目大意"></a>2.1 题目大意</h2><p>给定一个由$n$个小写拉丁字母组成的字符串$s$。至多删除一个字符，问删除后字典序最小的字符串是什么。</p>
<p>注意 “aaa” is smaller than “aaaa”, “abb” is smaller than “abc”, “pqr” is smaller than “z”</p>
<h2 id="2-2-解题思路"><a href="#2-2-解题思路" class="headerlink" title="2.2 解题思路"></a>2.2 解题思路</h2><p>由于$aaa$小于$aaaa$。我们可以考虑每个字符串后有一个比任何字母都要小的哨兵字符存在。</p>
<p>由于字典序的定义，我们可以知道 $abcda$ 的字符串删除一个字符后最小的字符串为$abca$。</p>
<p>$”abca” &lt; “abcd” &lt; “abcda” &lt; “abda” &lt; “acda” &lt; “bcda”$</p>
<p>因此我们可以将每个字符串看做一个数字序列 a 为 1， b 为 2， … … z 为26， 末尾哨兵为0;</p>
<p>很容易想到，在这个数字序列中，我们删除第一个下降位置的前半部分对应的字母一定可以获得最小字符串。</p>
<p>比如刚刚的 $abcda$ 对应 $123410$ 第一个下降位置为 $41$ 因此我们选择删除 $4$ 对应的字母 $d$ 可以获得最小字符串</p>
<h2 id="2-3-代码实现"><a href="#2-3-代码实现" class="headerlink" title="2.3 代码实现"></a>2.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 21时15分51秒</span></span><br><span class="line"><span class="comment">File Name     :B.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="keyword">char</span> s[maxn];</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">char</span>&gt; ans;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    <span class="keyword">int</span> n; sd(n);</span><br><span class="line">    ss(s); <span class="keyword">int</span> len = <span class="built_in">strlen</span>(s);</span><br><span class="line">    <span class="keyword">bool</span> ok = <span class="literal">false</span>;</span><br><span class="line">    rep(i, <span class="number">0</span>, len)&#123;</span><br><span class="line">        <span class="keyword">if</span>(i == len - <span class="number">1</span> || s[i] &gt; s[i + <span class="number">1</span>])&#123;</span><br><span class="line">            <span class="keyword">if</span>(ok) ans.pb(s[i]);</span><br><span class="line">            <span class="keyword">else</span> ok = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125; </span><br><span class="line">        ans.pb(s[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    rep(i, <span class="number">0</span>, len - <span class="number">1</span>) <span class="built_in">printf</span>(<span class="string">"%c"</span>, ans[i]);</span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">""</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="3-C-Divisor-Subtraction-CodeForces-1076B"><a href="#3-C-Divisor-Subtraction-CodeForces-1076B" class="headerlink" title="3 C - Divisor Subtraction CodeForces - 1076B"></a>3 C - Divisor Subtraction CodeForces - 1076B</h1><h2 id="3-1-题目大意"><a href="#3-1-题目大意" class="headerlink" title="3.1 题目大意"></a>3.1 题目大意</h2><p>给定一个范围在$1$ 到 $10 ^ {10}$ 的数字 $n$</p>
<p>算法将对其重复进行下述操作</p>
<p>如果 n == 0,，算法结束</p>
<p>否则的话，将这个数减去它的最大质因子</p>
<p>这个算法结束时我们进行了多少次减法</p>
<h2 id="3-2-解题思路"><a href="#3-2-解题思路" class="headerlink" title="3.2 解题思路"></a>3.2 解题思路</h2><p>如果这个数为偶数，它的最小质因子就是 $2$ </p>
<p>在每次减去 $2$ 之后，它将仍然是偶数</p>
<p>也就是说，一个偶数，我们只会将其进行减2操作</p>
<p>所以答案将会是 $ans = \frac{n}{2}$</p>
<p>如果这个数是奇数，那么它的最小质因子一定是个奇数</p>
<p>而在减去这个最小质因子后，也就是 奇数减去奇数 后，我们一定会获得一个偶数</p>
<p>因此答案将会是 $ans = \frac{a - p}{2} + 1$</p>
<h2 id="3-3-代码实现"><a href="#3-3-代码实现" class="headerlink" title="3.3 代码实现"></a>3.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 21时20分15秒</span></span><br><span class="line"><span class="comment">File Name     :C.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    ll n; sld(n);</span><br><span class="line">    <span class="keyword">if</span>((n&amp;<span class="number">1</span>) == <span class="number">0</span>)&#123;</span><br><span class="line">        pld(n/<span class="number">2</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;    </span><br><span class="line">    ll ans = <span class="number">1</span>;</span><br><span class="line">    ll sn = <span class="built_in">sqrt</span>(n), p = <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">for</span>(ll i = <span class="number">3</span>; i &lt;= sn; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(n%i) <span class="keyword">continue</span>;</span><br><span class="line">        p = i;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(p == <span class="number">-1</span>) p = n;</span><br><span class="line">    n -= p;</span><br><span class="line">    ans += n/<span class="number">2</span>;</span><br><span class="line">    pld(ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="4-D-Commentary-Boxes-CodeForces-990A"><a href="#4-D-Commentary-Boxes-CodeForces-990A" class="headerlink" title="4 D - Commentary Boxes CodeForces - 990A"></a>4 D - Commentary Boxes CodeForces - 990A</h1><h2 id="4-1-题目大意"><a href="#4-1-题目大意" class="headerlink" title="4.1 题目大意"></a>4.1 题目大意</h2><p>给定n个会场，m个代表团</p>
<p>每个代表团必须分配相同个数的会场（0个也可以）</p>
<p>我们可以花费 a 建设一个会场， m 拆毁一个会场</p>
<p>问满足题意的最小花费是多少</p>
<h2 id="4-2-解题思路"><a href="#4-2-解题思路" class="headerlink" title="4.2 解题思路"></a>4.2 解题思路</h2><p>每一次我们都只会拆除，或者只会建设，不会既拆除又建设</p>
<p>这样的话，我们考虑余数$rem = n\%m$，通过建设满足题意的花费$c1 = (m - rem)<em>a$，通过拆除的的花费为$c2 = rem</em>b$</p>
<p>这样的话答案将是$ans = min(c1, c2)$</p>
<h2 id="4-3-代码实现"><a href="#4-3-代码实现" class="headerlink" title="4.3 代码实现"></a>4.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 21时27分15秒</span></span><br><span class="line"><span class="comment">File Name     :D.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    ll n, m; sldd(n, m);</span><br><span class="line">    ll a, b; sldd(a, b);</span><br><span class="line">    ll rm = n%m;</span><br><span class="line">    ll c1 = rm*b, c2 = (m - rm)*a;</span><br><span class="line">    ll ans = min(c1, c2);</span><br><span class="line">    pld(ans);   </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="5-E-Maximum-Diameter-Graph-CodeForces-1082D"><a href="#5-E-Maximum-Diameter-Graph-CodeForces-1082D" class="headerlink" title="5 E - Maximum Diameter Graph CodeForces - 1082D"></a>5 E - Maximum Diameter Graph CodeForces - 1082D</h1><h2 id="5-1-题目大意"><a href="#5-1-题目大意" class="headerlink" title="5.1 题目大意"></a>5.1 题目大意</h2><p>给定n个点，每个点的最大度数为$a_i$</p>
<p>问这个图能否构造出来</p>
<p>能构造出来的话，构造出来的最大直径是多少，它的边是哪些。</p>
<h2 id="5-2-解题思路"><a href="#5-2-解题思路" class="headerlink" title="5.2 解题思路"></a>5.2 解题思路</h2><p>如果这个图存在，我们将其所有最大度数大于一的点连成一条线</p>
<p>如果存在度数为1的点，首先在连成的线最左边和最右边的点各放置一个</p>
<p>然后将剩下的度数为1的点连在度数还没超的线上的点即可</p>
<p>如果最后度数为1的点没有连完，那么给定的图一定不存在</p>
<h2 id="5-3-代码实现"><a href="#5-3-代码实现" class="headerlink" title="5.3 代码实现"></a>5.3 代码实现</h2><p>写的时候头有点晕，思路有点乱，大家凑合着看叭</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月07日 星期四 10时22分52秒</span></span><br><span class="line"><span class="comment">File Name     :E.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">5e2</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> a, id;</span><br><span class="line">    <span class="keyword">bool</span> <span class="keyword">operator</span>&lt;(<span class="keyword">const</span> node&amp; nd) <span class="keyword">const</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a &gt; nd.a;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;nd[maxn];</span><br><span class="line"><span class="keyword">int</span> d[maxn];</span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; u, v;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>&#123;</span><br><span class="line">    u.pb(x), v.pb(y);</span><br><span class="line">    --d[x], --d[y];</span><br><span class="line">    ++ans;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    <span class="keyword">int</span> n; sd(n);</span><br><span class="line">    rep(i, <span class="number">0</span>, n)&#123;</span><br><span class="line">        nd[i].id = i + <span class="number">1</span>;</span><br><span class="line">        sd(nd[i].a);</span><br><span class="line">        d[i + <span class="number">1</span>] = nd[i].a;</span><br><span class="line">    &#125;</span><br><span class="line">    sort(nd, nd + n);</span><br><span class="line">    <span class="keyword">if</span>(nd[<span class="number">0</span>].a == <span class="number">1</span>)&#123;</span><br><span class="line">        <span class="built_in">puts</span>(<span class="string">"NO"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> lst = <span class="number">-1</span>, pos = <span class="number">0</span>, rt = nd[<span class="number">0</span>].id;</span><br><span class="line">    rep(i, <span class="number">1</span>, n)&#123;</span><br><span class="line">        <span class="keyword">int</span> now = nd[i].id;</span><br><span class="line">        <span class="keyword">if</span>(d[now] == <span class="number">1</span>) <span class="keyword">break</span>;</span><br><span class="line">        pos = i;</span><br><span class="line">        <span class="keyword">if</span>(lst != <span class="number">-1</span>) solve(lst, now);</span><br><span class="line">        lst = now;</span><br><span class="line">    &#125;</span><br><span class="line">    ++pos;</span><br><span class="line">    <span class="keyword">if</span>(lst == <span class="number">-1</span>)&#123;</span><br><span class="line">        <span class="keyword">bool</span> ok = <span class="literal">true</span>;</span><br><span class="line">        rep(i, pos, n)&#123;</span><br><span class="line">            <span class="keyword">if</span>(d[rt] &lt;= <span class="number">0</span>)&#123;</span><br><span class="line">                ok = <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> now = nd[i].id;</span><br><span class="line">            solve(now, rt);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(!ok)&#123;</span><br><span class="line">            <span class="built_in">puts</span>(<span class="string">"NO"</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(ans &gt; <span class="number">2</span>) ans = <span class="number">2</span>;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"YES %d\n"</span>, ans);</span><br><span class="line">        <span class="keyword">int</span> sz = (<span class="keyword">int</span>)u.size();</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sz);</span><br><span class="line">        rep(i, <span class="number">0</span>, sz) <span class="built_in">printf</span>(<span class="string">"%d %d\n"</span>, u[i], v[i]);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> lf = nd[<span class="number">1</span>].id;</span><br><span class="line"><span class="comment">//    printf("%d %d %d\n", lf, lst, rt);</span></span><br><span class="line"><span class="comment">//    pd(pos);</span></span><br><span class="line">    solve(lst, rt);</span><br><span class="line">    <span class="keyword">if</span>(pos &lt; n)&#123;</span><br><span class="line">        solve(nd[pos++].id, lf);</span><br><span class="line">        <span class="keyword">if</span>(pos &lt; n) solve(nd[pos++].id, rt);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> now = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">bool</span> ok = <span class="literal">true</span>;</span><br><span class="line">    rep(i, pos, n)&#123;</span><br><span class="line">        <span class="keyword">while</span>(now &lt; n)&#123;</span><br><span class="line">            <span class="keyword">if</span>(d[nd[now].id] &gt; <span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">            ++now;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(now &gt;= i || now &gt;= n)&#123;</span><br><span class="line">            ok = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        solve(nd[i].id, nd[now].id);</span><br><span class="line">        --ans;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(!ok)&#123;</span><br><span class="line">        <span class="built_in">puts</span>(<span class="string">"NO"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"YES %d\n"</span>, ans);</span><br><span class="line">    <span class="keyword">int</span> sz = (<span class="keyword">int</span>)u.size();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sz);</span><br><span class="line">    rep(i, <span class="number">0</span>, sz) <span class="built_in">printf</span>(<span class="string">"%d %d\n"</span>, u[i], v[i]);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">3</span></span><br><span class="line"><span class="comment">2 2 2</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="6-F-Micro-World-CodeForces-990B"><a href="#6-F-Micro-World-CodeForces-990B" class="headerlink" title="6 F - Micro-World CodeForces - 990B"></a>6 F - Micro-World CodeForces - 990B</h1><h2 id="6-1-题目大意"><a href="#6-1-题目大意" class="headerlink" title="6.1 题目大意"></a>6.1 题目大意</h2><p>给定n个细菌，一个吞噬阈值k，每个细菌都有一个体积$a_i$</p>
<p>一个细菌能吞噬所有体积小于它但是体积差小于等于k的细菌</p>
<p>你可以规划吞噬顺序，问最后能剩余的最少细菌是多少个</p>
<h2 id="6-2-解题思路"><a href="#6-2-解题思路" class="headerlink" title="6.2 解题思路"></a>6.2 解题思路</h2><p>将细菌们按照体积升序排序</p>
<p>考虑每个细菌，二分查找第一个大于它的细菌，如果它们的体积差不大于k，这个细菌将可以被吃掉，顺手统计一下即可</p>
<h2 id="6-3-代码实现"><a href="#6-3-代码实现" class="headerlink" title="6.3 代码实现"></a>6.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 21时54分11秒</span></span><br><span class="line"><span class="comment">File Name     :F.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="keyword">int</span> a[maxn];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    <span class="keyword">int</span> n, k; sdd(n, k);    </span><br><span class="line">    rep(i, <span class="number">0</span>, n) sd(a[i]);</span><br><span class="line">    sort(a, a + n);</span><br><span class="line">    <span class="keyword">int</span> ans = n;</span><br><span class="line">    rep(i, <span class="number">0</span>, n - <span class="number">1</span>)&#123;</span><br><span class="line">        <span class="keyword">int</span> pos = upper_bound(a + i + <span class="number">1</span>, a + n, a[i]) - a;</span><br><span class="line">        <span class="keyword">if</span>(pos != n &amp;&amp; a[i] + k &gt;= a[pos]) --ans;</span><br><span class="line">    &#125; </span><br><span class="line">    pd(ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="7-G-A-Blend-of-Springtime-CodeForces-989A"><a href="#7-G-A-Blend-of-Springtime-CodeForces-989A" class="headerlink" title="7 G - A Blend of Springtime CodeForces - 989A"></a>7 G - A Blend of Springtime CodeForces - 989A</h1><h2 id="7-1-题目大意"><a href="#7-1-题目大意" class="headerlink" title="7.1 题目大意"></a>7.1 题目大意</h2><p>给定n个花盆，每个花盆种着A、B、C三种花朵，或者为空，花凋零后花瓣会掉入旁边的花盆，此花盆将不再存在这个花瓣</p>
<p>你可以调整花瓣凋零顺序</p>
<p>问是否有某个时刻，某个花盆存在三种颜色的花瓣</p>
<h2 id="7-2-解题思路"><a href="#7-2-解题思路" class="headerlink" title="7.2 解题思路"></a>7.2 解题思路</h2><p>根据题意，只要存在ABC连续存在的三个花盆，无论顺序如何，都将满足题意</p>
<p>我们只需在字符串里找一下是否存在连续的ABC子串</p>
<h2 id="7-3-代码实现"><a href="#7-3-代码实现" class="headerlink" title="7.3 代码实现"></a>7.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 21时31分54秒</span></span><br><span class="line"><span class="comment">File Name     :G.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="keyword">char</span> s[maxn];</span><br><span class="line"><span class="keyword">int</span> cnt[<span class="number">5</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    ss(s); <span class="keyword">int</span> len = <span class="built_in">strlen</span>(s);</span><br><span class="line">    <span class="keyword">int</span> tot = <span class="number">0</span>;</span><br><span class="line">    rep(i, <span class="number">0</span>, len)&#123;</span><br><span class="line"><span class="comment">//        printf("%d %d\n", i - 1, tot);</span></span><br><span class="line"><span class="comment">//        rep(j, 0, 3)&#123;</span></span><br><span class="line"><span class="comment">//            printf("%c %d\n", j + 'A', cnt[j]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">        <span class="keyword">if</span>(tot == <span class="number">3</span>)&#123;</span><br><span class="line">            <span class="built_in">puts</span>(<span class="string">"Yes"</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(s[i] != <span class="string">'.'</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span>&amp; now = cnt[s[i] - <span class="string">'A'</span>]; </span><br><span class="line">            <span class="keyword">if</span>(now == <span class="number">0</span>) ++tot;</span><br><span class="line">            ++now;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(i &lt; <span class="number">3</span> || s[i - <span class="number">3</span>] == <span class="string">'.'</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">int</span>&amp; lst = cnt[s[i - <span class="number">3</span>] - <span class="string">'A'</span>];</span><br><span class="line">        --lst;</span><br><span class="line">        <span class="keyword">if</span>(lst == <span class="number">0</span>) --tot;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(tot == <span class="number">3</span>)&#123;</span><br><span class="line">        <span class="built_in">puts</span>(<span class="string">"Yes"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"No"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="8-H-Trial-for-Chief-CodeForces-37E"><a href="#8-H-Trial-for-Chief-CodeForces-37E" class="headerlink" title="8 H - Trial for Chief CodeForces - 37E"></a>8 H - Trial for Chief CodeForces - 37E</h1><h2 id="8-1-题目大意"><a href="#8-1-题目大意" class="headerlink" title="8.1 题目大意"></a>8.1 题目大意</h2><p>给定一个全白色的$n \times m$的板</p>
<p>你每次可以将一个（可以是某个联通子区域）相同颜色的（上下左右四联通的）联通块涂成相反的颜色</p>
<p>问你最少需要几步可以将其涂成给定的颜色模式</p>
<h2 id="8-2-解题思路"><a href="#8-2-解题思路" class="headerlink" title="8.2 解题思路"></a>8.2 解题思路</h2><p>如果是一个全白色的板，明显答案是0</p>
<p>否则的话，我们可以枚举所有位置</p>
<p>考虑以某个位置(x, y)作为染色起点，我们所需要的最少花费</p>
<p>这样的话，我们只需将所有位置的花费取一个最小即可</p>
<p>如何计算以某个点为染色起点的最小花费呢</p>
<p>我们可以考虑这样一种染色方式，我们计算出所有点按照距离该位置的层数距离</p>
<p>距离层数的计算可以抽象为同一个颜色的相邻位置移动花费为0，不同颜色的相邻位置移动花费为1，跑一个迪杰斯特拉即可得到</p>
<p>首先将层数距离最远的黑点包含进去涂黑</p>
<p>将除离该位置最远的黑点外的所有点涂白，将包含所有次远黑点以及更近的黑点涂黑</p>
<p>重复上述操作，直至将所有所有点染成相应的颜色</p>
<p>这样的话，我们的花费是，距离此位置的最远的黑点加一</p>
<p>可以证明，这种染色方式一定保证每次染得都是联通块，而且次数是最少的</p>
<h2 id="8-3-代码实现"><a href="#8-3-代码实现" class="headerlink" title="8.3 代码实现"></a>8.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月07日 星期四 14时58分01秒</span></span><br><span class="line"><span class="comment">File Name     :H.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="keyword">int</span> tox[] = &#123;<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">-1</span>&#125;;</span><br><span class="line"><span class="keyword">int</span> toy[] = &#123;<span class="number">1</span>, <span class="number">-1</span>, <span class="number">0</span>, <span class="number">0</span>&#125;;</span><br><span class="line"><span class="keyword">char</span> g[<span class="number">55</span>][<span class="number">55</span>];</span><br><span class="line"><span class="keyword">int</span> n, m, dist[<span class="number">55</span>][<span class="number">55</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(x &lt; <span class="number">0</span> || x &gt;= n) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span>(y &lt; <span class="number">0</span> || y &gt;= m) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> x, y, t;</span><br><span class="line">    <span class="keyword">bool</span> <span class="keyword">operator</span> &lt;(<span class="keyword">const</span> node&amp; nd) <span class="keyword">const</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> t &gt; nd.t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dijk</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> ret = <span class="number">0</span>, t;</span><br><span class="line">    priority_queue&lt;node&gt; q;</span><br><span class="line">    node now; now.t = <span class="number">1</span>, now.x = x, now.y = y;</span><br><span class="line">    q.push(now);</span><br><span class="line">    dist[x][y] = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span>(!q.empty())&#123;</span><br><span class="line">        now = q.top(); q.pop();</span><br><span class="line">        rep(i, <span class="number">0</span>, <span class="number">4</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span> tx = now.x + tox[i], ty = now.y + toy[i], tt = now.t;</span><br><span class="line">            <span class="keyword">if</span>(!judge(tx, ty)) <span class="keyword">continue</span>;</span><br><span class="line">            <span class="keyword">if</span>(g[tx][ty] != g[now.x][now.y]) ++tt;</span><br><span class="line">            <span class="keyword">if</span>(tt &gt;= dist[tx][ty]) <span class="keyword">continue</span>;</span><br><span class="line">            node nxt; nxt.x = tx, nxt.y = ty, nxt.t = tt;</span><br><span class="line">            dist[tx][ty] = tt;</span><br><span class="line">            q.push(nxt);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    rep(i, <span class="number">0</span>, n)&#123;</span><br><span class="line">        rep(j, <span class="number">0</span>, m)&#123;</span><br><span class="line">            <span class="keyword">if</span>(g[i][j] == <span class="string">'W'</span>) <span class="keyword">continue</span>;</span><br><span class="line">            <span class="keyword">if</span>(dist[i][j] &gt; ret) ret = dist[i][j];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    sdd(n, m);    </span><br><span class="line">    <span class="keyword">bool</span> ok = <span class="literal">true</span>;</span><br><span class="line">    rep(i, <span class="number">0</span>, n)&#123;</span><br><span class="line">        ss(g[i]);</span><br><span class="line">        rep(j, <span class="number">0</span>, m)&#123;</span><br><span class="line">            <span class="keyword">if</span>(g[i][j] == <span class="string">'W'</span>) <span class="keyword">continue</span>;</span><br><span class="line">            ok = <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(ok)&#123;</span><br><span class="line">        <span class="built_in">puts</span>(<span class="string">"0"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> ans = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line">    rep(i, <span class="number">0</span>, n)&#123;</span><br><span class="line">        rep(j, <span class="number">0</span>, m)&#123;</span><br><span class="line">            rep(ii, <span class="number">0</span>, n) rep(jj, <span class="number">0</span>, m) dist[ii][jj] = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line">            <span class="keyword">int</span> now = dijk(i, j);</span><br><span class="line">            <span class="keyword">if</span>(now &lt; ans) ans = now;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; </span><br><span class="line">    pd(ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="9-I-A-Tide-of-Riverscape-CodeForces-989B"><a href="#9-I-A-Tide-of-Riverscape-CodeForces-989B" class="headerlink" title="9 I - A Tide of Riverscape CodeForces - 989B"></a>9 I - A Tide of Riverscape CodeForces - 989B</h1><h2 id="9-1-题目大意"><a href="#9-1-题目大意" class="headerlink" title="9.1 题目大意"></a>9.1 题目大意</h2><p>给定一个长度为n的01字符串，’.’位置你可以选择填0或者1,</p>
<p>问是否存在一个字符串，满足不以p为循环节</p>
<h2 id="9-2-解题思路"><a href="#9-2-解题思路" class="headerlink" title="9.2 解题思路"></a>9.2 解题思路</h2><p>我们只需找到一个位置，它的下p个位置，有可能与这个位置不同</p>
<p>有以下几种情况</p>
<p>此位置是1，它的下p个位置是0</p>
<p>此位置是0，它的下p个位置是1</p>
<p>此位置或者它的下p个位置是’.’</p>
<p>均可满足题意</p>
<p>将对应位置填成相反的数字，其余位置随便填即可</p>
<p>如果不存在这样的位置，即不存在满足题意的字符串</p>
<h2 id="9-3-代码实现"><a href="#9-3-代码实现" class="headerlink" title="9.3 代码实现"></a>9.3 代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* ***********************************************</span></span><br><span class="line"><span class="comment">Author        :linxi</span></span><br><span class="line"><span class="comment">Created Time  :2019年02月06日 星期三 21时31分54秒</span></span><br><span class="line"><span class="comment">File Name     :G.cpp</span></span><br><span class="line"><span class="comment">************************************************ */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sd(n) scanf(<span class="meta-string">"%d"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sdd(n,m) scanf(<span class="meta-string">"%d%d"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sddd(n,m,k) scanf(<span class="meta-string">"%d%d%d"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pd(n) printf(<span class="meta-string">"%d\n"</span>, (n))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pdd(n,m) printf(<span class="meta-string">"%d %d"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pld(n) printf(<span class="meta-string">"%lld\n"</span>, n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pldd(n,m) printf(<span class="meta-string">"%lld %lld\n"</span>, n, m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sld(n) scanf(<span class="meta-string">"%lld"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sldd(n,m) scanf(<span class="meta-string">"%lld%lld"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> slddd(n,m,k) scanf(<span class="meta-string">"%lld%lld%lld"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sf(n) scanf(<span class="meta-string">"%lf"</span>,&amp;n)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sff(n,m) scanf(<span class="meta-string">"%lf%lf"</span>,&amp;n,&amp;m)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sfff(n,m,k) scanf(<span class="meta-string">"%lf%lf%lf"</span>,&amp;n,&amp;m,&amp;k)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ss(str) scanf(<span class="meta-string">"%s"</span>,str)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,a,n) for (int i=a;i&lt;n;i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,a,n) for (int i=n-1;i&gt;=a;i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mm(a,n) memset(a, n, sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> debug(x) cout&lt;&lt;#x&lt;&lt;<span class="meta-string">": "</span>&lt;&lt;x&lt;&lt;endl</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> all(x) (x).begin(),(x).end()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fi first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> se second</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mp make_pair()</span></span><br><span class="line"><span class="keyword">typedef</span> pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">double</span> ld;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">3e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="comment">//head</span></span><br><span class="line"><span class="keyword">char</span> s[maxn];</span><br><span class="line"><span class="keyword">int</span> cnt[<span class="number">5</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//freopen("in.txt","r",stdin);</span></span><br><span class="line">	<span class="comment">//freopen("out.txt","w",stdout);</span></span><br><span class="line">    ss(s); <span class="keyword">int</span> len = <span class="built_in">strlen</span>(s);</span><br><span class="line">    <span class="keyword">int</span> tot = <span class="number">0</span>;</span><br><span class="line">    rep(i, <span class="number">0</span>, len)&#123;</span><br><span class="line"><span class="comment">//        printf("%d %d\n", i - 1, tot);</span></span><br><span class="line"><span class="comment">//        rep(j, 0, 3)&#123;</span></span><br><span class="line"><span class="comment">//            printf("%c %d\n", j + 'A', cnt[j]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">        <span class="keyword">if</span>(tot == <span class="number">3</span>)&#123;</span><br><span class="line">            <span class="built_in">puts</span>(<span class="string">"Yes"</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(s[i] != <span class="string">'.'</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span>&amp; now = cnt[s[i] - <span class="string">'A'</span>]; </span><br><span class="line">            <span class="keyword">if</span>(now == <span class="number">0</span>) ++tot;</span><br><span class="line">            ++now;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(i &lt; <span class="number">3</span> || s[i - <span class="number">3</span>] == <span class="string">'.'</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">int</span>&amp; lst = cnt[s[i - <span class="number">3</span>] - <span class="string">'A'</span>];</span><br><span class="line">        --lst;</span><br><span class="line">        <span class="keyword">if</span>(lst == <span class="number">0</span>) --tot;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(tot == <span class="number">3</span>)&#123;</span><br><span class="line">        <span class="built_in">puts</span>(<span class="string">"Yes"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"No"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/ACM/" rel="tag"># ACM</a>
          
            <a href="/tags/Tutorial/" rel="tag"># Tutorial</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20190211/ACM-Head/" rel="next" title="ACM Head">
                <i class="fa fa-chevron-left"></i> ACM Head
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20190211/ACM动态规划基础篇/" rel="prev" title="ACM动态规划基础篇">
                ACM动态规划基础篇 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/star.jpg" alt="linxi">
            
              <p class="site-author-name" itemprop="name">linxi</p>
              <p class="site-description motion-element" itemprop="description">stay hungry, stay foolish</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">15</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/linxilinxilinxi" title="https://blog.csdn.net/linxilinxilinxi" rel="noopener" target="_blank">My CSDN</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-A-Multi-Subject-Competition-CodeForces-1082C"><span class="nav-number">1.</span> <span class="nav-text">1. A - Multi-Subject Competition CodeForces - 1082C</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-题目大意"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-解题思路"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-代码实现"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-B-Minimizing-the-String-CodeForces-1076A"><span class="nav-number">2.</span> <span class="nav-text">2 B - Minimizing the String CodeForces - 1076A</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-题目大意"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-解题思路"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-代码实现"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-C-Divisor-Subtraction-CodeForces-1076B"><span class="nav-number">3.</span> <span class="nav-text">3 C - Divisor Subtraction CodeForces - 1076B</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-题目大意"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-解题思路"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-代码实现"><span class="nav-number">3.3.</span> <span class="nav-text">3.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-D-Commentary-Boxes-CodeForces-990A"><span class="nav-number">4.</span> <span class="nav-text">4 D - Commentary Boxes CodeForces - 990A</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-题目大意"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-解题思路"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-代码实现"><span class="nav-number">4.3.</span> <span class="nav-text">4.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-E-Maximum-Diameter-Graph-CodeForces-1082D"><span class="nav-number">5.</span> <span class="nav-text">5 E - Maximum Diameter Graph CodeForces - 1082D</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-题目大意"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-解题思路"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-3-代码实现"><span class="nav-number">5.3.</span> <span class="nav-text">5.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-F-Micro-World-CodeForces-990B"><span class="nav-number">6.</span> <span class="nav-text">6 F - Micro-World CodeForces - 990B</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#6-1-题目大意"><span class="nav-number">6.1.</span> <span class="nav-text">6.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-2-解题思路"><span class="nav-number">6.2.</span> <span class="nav-text">6.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-3-代码实现"><span class="nav-number">6.3.</span> <span class="nav-text">6.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7-G-A-Blend-of-Springtime-CodeForces-989A"><span class="nav-number">7.</span> <span class="nav-text">7 G - A Blend of Springtime CodeForces - 989A</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#7-1-题目大意"><span class="nav-number">7.1.</span> <span class="nav-text">7.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-2-解题思路"><span class="nav-number">7.2.</span> <span class="nav-text">7.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-3-代码实现"><span class="nav-number">7.3.</span> <span class="nav-text">7.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#8-H-Trial-for-Chief-CodeForces-37E"><span class="nav-number">8.</span> <span class="nav-text">8 H - Trial for Chief CodeForces - 37E</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#8-1-题目大意"><span class="nav-number">8.1.</span> <span class="nav-text">8.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-2-解题思路"><span class="nav-number">8.2.</span> <span class="nav-text">8.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-3-代码实现"><span class="nav-number">8.3.</span> <span class="nav-text">8.3 代码实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#9-I-A-Tide-of-Riverscape-CodeForces-989B"><span class="nav-number">9.</span> <span class="nav-text">9 I - A Tide of Riverscape CodeForces - 989B</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#9-1-题目大意"><span class="nav-number">9.1.</span> <span class="nav-text">9.1 题目大意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-2-解题思路"><span class="nav-number">9.2.</span> <span class="nav-text">9.2 解题思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-3-代码实现"><span class="nav-number">9.3.</span> <span class="nav-text">9.3 代码实现</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2019 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">linxi</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.0.0</div>





<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 65.5k 字</span>
</div>

        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>














  
    
    
  
  <script color="25,202,173" opacity="0.8" zindex="-1" count="200" src="/lib/canvas-nest/canvas-nest.min.js"></script>













  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.0"></script>

  <script src="/js/src/motion.js?v=7.0.0"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.0"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.0"></script>



  
  <script src="/js/src/scrollspy.js?v=7.0.0"></script>
<script src="/js/src/post-details.js?v=7.0.0"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.0"></script>



  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
    appKey: 'akTaTqH7XeeKc6CPRfB7vbDl',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false
  });
</script>




  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  
  
  <script>
    
    function addCount(Counter) {
      var $visitors = $('.leancloud_visitors');
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
            Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
            
              .done(function() {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.time + 1);
              })
            
              .fail(function ({ responseJSON }) {
                console.log(`Failed to save Visitor num, with error message: ${responseJSON.error}`);
              })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
                .done(function() {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function() {
                  console.log('Failed to create');
                });
            
          }
        })
        .fail(function ({ responseJSON }) {
          console.log(`LeanCloud Counter Error: ${responseJSON.code} ${responseJSON.error}`);
        });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz')
        .done(function({ api_server }) {
          var Counter = function(method, url, data) {
            return $.ajax({
              method: method,
              url: 'https://' + api_server + '/1.1' + url,
              headers: {
                'X-LC-Id': 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
                'X-LC-Key': 'akTaTqH7XeeKc6CPRfB7vbDl',
                'Content-Type': 'application/json',
              },
              data: data
            });
          };
          
            addCount(Counter);
          
        });
    });
  </script>



  

  
  

  
  

  
    
      <script type="text/x-mathjax-config">
  

  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true,
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      
      equationNumbers: {
        autoNumber: "AMS"
      }
    }
  });
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
      for (i = 0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>
<script src="//cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<style>
.MathJax_Display {
  overflow: auto hidden;
}
</style>

    
  


  

  

  

  

  

  

  

  

  

  

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>

